package cn.iocoder.yudao.module.member.controller.admin.companyAccessCount;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.companyAccessCount.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.companyAccessCount.CompanyAccessCountDO;
import cn.iocoder.yudao.module.member.convert.companyAccessCount.CompanyAccessCountConvert;
import cn.iocoder.yudao.module.member.service.companyAccessCount.CompanyAccessCountService;

@Api(tags = "管理后台 -运营端- 访问记录")
@RestController
@RequestMapping("/member/company-access-count")
@Validated
public class CompanyAccessCountController {

    @Resource
    private CompanyAccessCountService companyAccessCountService;

    @PostMapping("/createCompanyAccessCountIfPresent")
    @ApiOperation("更新访问记录 （没有就创建）")
    public CommonResult<Long> createCompanyAccessCountIfPresent(@Valid CompanyAccessCountUpdateReqVO updateReqVO) {
        return success(companyAccessCountService.createCompanyAccessCountIfPresent(updateReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新访问记录")
    public CommonResult<Boolean> updateCompanyAccessCount(@Valid CompanyAccessCountUpdateReqVO updateReqVO) {
        companyAccessCountService.updateCompanyAccessCount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除访问记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:company-access-count:delete')")
    public CommonResult<Boolean> deleteCompanyAccessCount(@RequestParam("id") Long id) {
        companyAccessCountService.deleteCompanyAccessCount(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得访问记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('member:company-access-count:query')")
    public CommonResult<CompanyAccessCountRespVO> getCompanyAccessCount(@RequestParam("id") Long id) {
        CompanyAccessCountDO companyAccessCount = companyAccessCountService.getCompanyAccessCount(id);
        return success(CompanyAccessCountConvert.INSTANCE.convert(companyAccessCount));
    }

    @GetMapping("/list")
    @ApiOperation("获得访问记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('member:company-access-count:query')")
    public CommonResult<List<CompanyAccessCountRespVO>> getCompanyAccessCountList(@RequestParam("ids") Collection<Long> ids) {
        List<CompanyAccessCountDO> list = companyAccessCountService.getCompanyAccessCountList(ids);
        return success(CompanyAccessCountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得访问记录分页")
    @PreAuthorize("@ss.hasPermission('member:company-access-count:query')")
    public CommonResult<PageResult<CompanyAccessCountRespVO>> getCompanyAccessCountPage(@Valid CompanyAccessCountPageReqVO pageVO) {
        PageResult<CompanyAccessCountDO> pageResult = companyAccessCountService.getCompanyAccessCountPage(pageVO);
        return success(CompanyAccessCountConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出访问记录 Excel")
    @PreAuthorize("@ss.hasPermission('member:company-access-count:export')")
    @OperateLog(type = EXPORT)
    public void exportCompanyAccessCountExcel(@Valid CompanyAccessCountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CompanyAccessCountDO> list = companyAccessCountService.getCompanyAccessCountList(exportReqVO);
        // 导出 Excel
        List<CompanyAccessCountExcelVO> datas = CompanyAccessCountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "访问记录.xls", "数据", CompanyAccessCountExcelVO.class, datas);
    }

}
